using System;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections.Generic;
using System.Data.Common;
using VSSAHT.Transfer;
using VSS_AHT.DataAccess;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
namespace VSSAHT.DataAccess
{
	public class ItemDataAccessBase : BaseDataAccess, IItemDataAccessBase
	{
		public IList<ItemDTO> GetAll()
		{
			IList<ItemDTO> list = null;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
					using(VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						list = biocode.Items.Select(e => ItemMapper.ToBusinessObject(e)).ToList();
					}
				}
			}
			catch
			{
			}
			return list;
		}
		public bool Add(ItemDTO dto)
		{
			bool isSuccess = false;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
					using(VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						if (OnBeforeAdd(biocode, dto))
						{
							var entity = ItemMapper.ToEntity(new Item(), dto);
							try
							{
								biocode.Items.InsertOnSubmit(entity);
								biocode.SubmitChanges();
								isSuccess = true;
							}
							catch(ChangeConflictException)
							{
								//  A possible concurrency exception occurred.  Let's see if
								//  we can resolve it.
								foreach(var conflict in biocode.ChangeConflicts)
								{
									conflict.Resolve(RefreshMode.KeepCurrentValues);
								}
								try
								{
									biocode.SubmitChanges();
									isSuccess = true;
								}
								catch(ChangeConflictException)
								{
									//  It didn't work, so throw a new exception.
									throw new Exception("A concurrency error occurred!");
								}
							}
							catch(Exception)
							{
								throw new Exception("There was an error saving this record!");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				_errorMessage = ex.Message;
			}
			return isSuccess;
		}
		public bool Edit(ItemDTO dto)
		{
            bool isSuccess = false;
            // note: here you can use any persistance library
            // to dynamically populate a list of all objects
            try
            {
                // Process
                using (IDbConnection connection = DbProviderFactory.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;
                    using (VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
                    {
                        if (OnBeforeEdit(biocode, dto))
                        {
                            var entity =ItemMapper.ToEntity(new Item(), dto);
                            try
                            {
                                biocode.Items.Attach(entity, true);
                                biocode.SubmitChanges();
                                isSuccess = true;
                            }
                            catch (ChangeConflictException)
                            {
                                //  A possible concurrency exception occurred.  Let's see if
                                //  we can resolve it.
                                foreach (var conflict in biocode.ChangeConflicts)
                                {
                                    conflict.Resolve(RefreshMode.KeepCurrentValues);
                                }
                                try
                                {
                                    biocode.SubmitChanges();
                                    isSuccess = true;
                                }
                                catch (ChangeConflictException)
                                {
                                    //  It didn't work, so throw a new exception.
                                    throw new Exception("A concurrency error occurred!");
                                }
                            }
                            catch (Exception)
                            {
                                throw new Exception("There was an error saving this record!");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _errorMessage = ex.Message;
            }
            return isSuccess;
		}


		public bool Delete(ItemDTO dto)
		{
            bool result = false;
            try
            {
                using (VSSAHTDataContext dataContext = new VSSAHTDataContext(base.ConnectionString))
                {
                    Item entity = dataContext.Items.Where(e => e.ItemNumber == dto.ItemNumber).SingleOrDefault();
                    dataContext.Items.DeleteOnSubmit(entity);
                    dataContext.SubmitChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
		}
		#region Extensibility Method Declarations

		protected virtual bool OnBeforeAdd(VSSAHTDataContext biocode, ItemDTO dto) { return true; }
		protected virtual bool OnBeforeEdit(VSSAHTDataContext biocode, ItemDTO dto) { return true; }
		protected virtual bool OnBeforeDelete(VSSAHTDataContext biocode, ItemDTO dto) { return true; }

		#endregion

	}
}
